<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <!-- 搜索栏 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable>
            <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 订单表格区域 -->
      <el-table :data="orderList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="order_id" label="订单编号"></el-table-column>
        <el-table-column prop="order_price" label="订单价格"></el-table-column>
        <el-table-column prop="pay_status" label="是否付款">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.pay_status === '0'">未付款</el-tag>
            <el-tag type="success" v-else>已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"></el-table-column>
        <el-table-column prop="create_time" label="下单时间">
          <template slot-scope="scope">{{scope.row.create_time | dateFormat }}</template>
        </el-table-column>
        <el-table-column label="操作" width="125px">
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="showEdit"></el-button>
          <el-button type="success" size="mini" icon="el-icon-location" @click="showProgress"></el-button>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[2, 5, 7, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- 修改地址对话框 -->
    <el-dialog title="修改地址" :visible.sync="editDialogVisible" width="50%" @close="addressDialogClosed">
      <el-form :model="addressForm" :rules="addressFormrules" ref="addressFormRef" label-width="100px">
        <!-- 添加表单 -->
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader v-model="addressForm.address1" :options="cityData"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 物流进度条对话框 -->
    <el-dialog title="查看物流进度" :visible.sync="logisticsDialogVisible" width="50%">
        <!-- 时间线组件 -->
        <el-timeline>
            <el-timeline-item
              v-for="(activity, index) in logisticsList"
              icon="el-icon-message-solid"
              color="blue"
              :key="index"
              :timestamp="activity.time">
              {{ activity.context }}
            </el-timeline-item>
        </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import cityData from "./citydata.js";
export default {
  data() {
    return {
      //请求参数对象
      queryInfo: {
        //查询参数
        query: "",
        //当前页码
        pagenum: 1,
        //每页显示条数
        pagesize: 10
      },
      //服务器返回的订单数据列表
      orderList: [],
      //数据总条数
      total: 0,
      //控制修改地址对话框的显示与隐藏
      editDialogVisible: false,
      //修改收货地址的表单
      addressForm: {
        address1: [],
        address2: "",
      },
      //编辑表单验证规则
      addressFormrules: {
        address1: [
          { required: true, message: "请选择省市区县", trigger: "blur" }
        ],
        address2: [
          { required: true, message: "请输入详细地址", trigger: "blur" }
        ]
      },
      //将导入的cityData数据保存起来
      cityData: cityData,
      //控制物流进度对话框显示与隐藏
      logisticsDialogVisible: false,
      //订单物流信息
      logisticsList: [],
    };
  },
  created() {
    this.getOrderList();
  },
  methods: {
    //获取订单数据
    async getOrderList() {
      const { data: res } = await this.$http.get("orders", {
        params: this.queryInfo
      });
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg);
      }
      //   this.$message.success(res.meta.msg);
      this.total = res.data.total;
      this.orderList = res.data.goods;
      //   console.log(this.orderList);
    },
    //每页条数变化时
    handleSizeChange(val) {
      this.queryInfo.pagesize = val;
      this.getOrderList();
    },
    //当前页码改变时
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val;
      this.getOrderList();
    },
    //点击修改按钮，显示对话框
    showEdit() {
      this.editDialogVisible = true;
    },
    //修改地址对话框关闭时触发事件
    addressDialogClosed(){
        //重置表单
        this.$refs.addressFormRef.resetFields()
    },
    //展示订单物流信息对话框
    async showProgress(){
        //发送请求获取物流数据
        const { data: res } = await this.$http.get('/kuaidi/804909574412544580')
        if(res.meta.status !== 200) {
            return this.$message.error(res.meta.message)
        }
        this.logisticsList = res.data
        // this.$message.success(res.meta.message)
        //显示对话框
        this.logisticsDialogVisible = true
    }
  }
};
</script>

<style lang="less" scoped>
.el-table {
  margin-top: 15px;
}
.el-cascader {
    width: 100%;
}
</style>